/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.wadministration;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import database.StaffDAO;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import model.Customer;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Member")
public class AccountAction extends ActionSupport implements ModelDriven<Staff>{
    private Staff staff = new Staff();
    private StaffDAO staffDAO = new StaffDAO();
    private String openTime;

    public String getOpenTime() {
        return openTime;
    }

    public void setOpenTime(String openTime) {
        this.openTime = openTime;
    }
    

    public Staff getStaff() {
        return staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }
    @Action(value="viewProfile",
             results = {
                 @Result(name="success", location="/manageMember/w_staffhome.jsp")
             })
     public String AddStaff()
     {
         return SUCCESS;
     }
    @Action(value = "editProfileForm", results = {
        @Result(name = "success", location = "/manageMember/w_staffedit.jsp"),
        @Result(name = "input", location = "/manageMember/w_staffedit.jsp")
    })
    //@SkipValidation
    public String editProfileForm() {
        Map session = ActionContext.getContext().getSession();
        //System.out.println("Trang"+(Staff) session.get("staff"));
        staff = (Staff) session.get("staff");
         SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        openTime = dateFormat.format(staff.getOpenTime());
        System.out.println("Staff"+openTime);
        return SUCCESS;
    }

    @Action(value = "editProfile", results = {
        @Result(name = "success", location = "/manageMember/w_staffEditSuccess.jsp"),
        @Result(name = "input", location = "/manageMember/w_staffedit.jsp")
    })
    public String editProfile() {
        Map session = ActionContext.getContext().getSession();
        Staff oldStaff = (Staff) session.get("staff");
        staff.setIdStaff(oldStaff.getIdStaff());
        System.out.println("ABC");
        if (staffDAO.updateStaff(staff)) {
            addActionMessage("Your Profile has been changed successfully. Please login again to access system");
            
            staff = staffDAO.getStaffByStaffId(oldStaff.getIdStaff());
            session.put("staff", staff);
            
        //session.remove("staff");
        //session.remove("currentCustomer");
            return SUCCESS;
        } else {
            addActionError("Cannot Update Profile. Please try again later.");
            return INPUT;
        }
    }
    @Override
    public Staff getModel() {
        return staff; //To change body of generated methods, choose Tools | Templates.
    }
}
